package com.hxy.platform.admin.controller;

import com.hxy.platform.admin.pojo.Gallery;
import com.hxy.platform.admin.service.GalleryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class GalleryController {
    @Autowired
    private GalleryService galleryService;

    /**
     * 查询所有服务
     */
    @RequestMapping("gallery/getAll")
    public String getAllGallery(ModelMap modelMap) {
        List<Gallery> galleries = galleryService.getAll();
        modelMap.addAttribute("galleries", galleries);
        return "gallery";
    }

    /**
     * 删除服务
     */
    @RequestMapping("gallery/deleteGallery")
    public String deleteGallery(@RequestParam("id") Integer id, ModelMap modelMap) {
        int result = galleryService.deleteGallery(id);
        if (result > 0) {
            List<Gallery> galleries = galleryService.getAll();
            modelMap.addAttribute("galleries", galleries);
            return "gallery";
        }
        return null;
    }
    /**
     * 添加服务
     */
    @RequestMapping("gallery/addGallery")
    public String addGallery(Gallery gallery, ModelMap modelMap) {
        if (StringUtils.isBlank(gallery.getAddress())) {
            return "addGallery";
        }
        int result = galleryService.addGallery(gallery);
        if (result > 0) {
            List<Gallery> galleries = galleryService.getAll();
            modelMap.addAttribute("galleries", galleries);
            return "gallery";
        }
        return "";
    }
}
